//
// Created by liang on 2025/4/6.
//

#include "WindowControl.h"

#include "SSUtil.h"
#include "QDebug"
using namespace SS;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
    std::map<HWND,TaskWindow>* windowMap = reinterpret_cast<std::map<HWND,TaskWindow>*>(lParam);
    const int MAX_TITLE_LENGTH = 256;
    wchar_t windowTitle[MAX_TITLE_LENGTH];
    // 获取窗口标题
    if (GetWindowText(hwnd, windowTitle, MAX_TITLE_LENGTH) > 0) {
        auto winTitleStr = QString::fromStdString(SSUtil::wchar_tToString(windowTitle));
        if (winTitleStr.startsWith("幻唐志 -")) {
            qDebug() << "窗口句柄:" << hwnd << "窗口标题:" << winTitleStr;
            TaskWindow taskWindow(winTitleStr, hwnd);
            windowMap->emplace(hwnd, std::move(taskWindow));
        }
    }
    return TRUE;
}

WindowControl::WindowControl() {
    hDll = LoadLibrary(L"D:\\software\\AutoIt3\\AutoItX\\AutoItX3_x64.dll");
}

WindowControl::~WindowControl() {
}

const std::map<HWND, SS::TaskWindow>& WindowControl::getAllWindows(){
    LPARAM param = reinterpret_cast<LPARAM>(&windowMap);
    EnumWindows(EnumWindowsProc, param);
    return windowMap;
}
